package com.cloudkinto.service.shipprice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.common.DicBusinessConstant;
import com.cloudkinto.common.DicBusinessItemConstant;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.DicBusinessItemDao;
import com.cloudkinto.dao.DimensDao;
import com.cloudkinto.entity.DicBusinessItemDo;
import com.cloudkinto.entity.DimensDo;
import com.cloudkinto.service.dic.DicBusinessItemService;
import com.cloudkinto.service.dic.vo.DicItemRes;
import com.cloudkinto.service.shipprice.DimensService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author ding
 * @since 2020-06-09
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class DimensServiceImpl extends ServiceImpl<DimensDao, DimensDo> implements DimensService {
    @Autowired
    private DimensDao dao;
    @Autowired
    DicBusinessItemService dicBusinessItemService;


    @Override
    public SingleResult addInit() {
        return SingleResult.success("");
    }

    @Override
    public DimensDo add(Object req, Long userId) {
        DimensDo entityDo = new DimensDo();
        BeanUtils.copyProperties(req, entityDo);
        handlerAddBefore(entityDo, userId);
        dao.insert(entityDo);
        return handlerAddAfter(entityDo);
    }

    //新增校验
    private void addCheck(Object req) {

    }

    //做新增之前要执行的业务定制处理
    private void handlerAddBefore(DimensDo entityDo, Long userId) {
//        entityDo.setCreateBy(userId);
//        entityDo.setCreateTime(new Date());
//        entityDo.setDeleteFlag(SysConstant.DELETE_FLAG_FALSE);
    }

    //将新增的do转换成要返回的实体
    private DimensDo handlerAddAfter(DimensDo entityDo) {
        DimensDo res = new DimensDo();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public SingleResult updateInit(Long id) {
        return SingleResult.success("");
    }

    @Override
    public Object update(Object req, Long userId) {
        /*DimensDo entityDo = dao.selectById(req.getId());
        BeanUtils.copyProperties(req, entityDo);
        handlerUpdateBefore(entityDo, userId);
        dao.updateById(entityDo);
        return handlerUpdateAfter(entityDo);*/
        return null;
    }

    //编辑校验
    private void updateCheck(Object req) {

    }

    //做修改之前要执行的业务定制处理
    private void handlerUpdateBefore(DimensDo entityDo, Long userId) {
//        entityDo.setUpdateBy(userId);
//        entityDo.setUpdateTime(new Date());
    }

    //做修改之后要执行的业务定制处理
    private Object handlerUpdateAfter(DimensDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public int delete(Long id, Long userId) {
        DimensDo entityDo = dao.selectById(id);
        handlerDeleteBefore(entityDo, userId);
        return dao.deleteById(id);
    }

    //做删除之前要执行的业务定制处理
    private void handlerDeleteBefore(DimensDo entityDo, Long userId) {
//        entityDo.setDeleteBy(userId);
//        entityDo.setDeleteTime(new Date());
    }

    @Override
    public Object detail(Long id) {
        DimensDo entityDo = dao.selectById(id);
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return handlerDetailAfter(res);
    }

    //查询明细之后要执行的业务定制处理
    private Object handlerDetailAfter(Object res) {
        return res;
    }

    @Override
    public SingleResult pageInit() {
        return SingleResult.success("");
    }

    @Override
    public List<DimensDo> getList(Map<String, String> map) {
        QueryWrapper<DimensDo> wrapper = queryBuild(map);
        List<DimensDo> list = this.list(wrapper);
        List<DimensDo> result = handlerListAfter(list);
        return result;
    }

    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<DimensDo> wrapper = queryBuild(map);
        IPage<DimensDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<DimensDo> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    //查询分页列表之后对列表数据的处理
    private List<DimensDo> handlerListAfter(List<DimensDo> list) {
        List<DimensDo> result = new ArrayList<>();
        return result;
    }

    private IPage<DimensDo> pageInit(Map<String, String> map) {
        IPage<DimensDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<DimensDo> queryBuild(Map<String, String> map) {
        QueryWrapper<DimensDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("keyword"))) {
            wrapper.like("keyword", map.get("keyword"));
        }
        return wrapper;
    }
    /**
     * 根据 尺寸 重量 从所有物流公司种 获取 dimensid
     *
     * @param size
     * @param weight
     * @param storageId
     * @return
     */
//    @Override
//    public List<Long> listBySizeAndWeight(double size, double weight, Long storageId) {
//        return this.listBySizeAndWeight(size, weight, null ,null, null, storageId);
//    }


    /**
     * 根据 尺寸 重量 从所有物流公司种 获取 dimensid 并且 对 最长边 次长边 最短边 有要求
     *
     * @param size
     * @param weight
     * @param storageId
     * @return
     */
    @Override
    public List<DimensDo> listBySizeAndWeight(double size, double weight, Double longest, Double minor, Double shortest, Integer cod, Integer shipmentType, Long storageId, Integer bundling) {
        List<DimensDo> ids = new ArrayList<>();
        List<DicItemRes> trackCompanyList = dicBusinessItemService.getDicItemList(DicBusinessConstant.trackCompany, true);
        for (DicItemRes trackCompany : trackCompanyList) {
            if (trackCompany.getId().equals(DicBusinessItemConstant.xinongExpress)) continue;
            DimensDo dimensId = baseMapper.queryBySizeAndWeight(size, weight, longest, minor, shortest, cod, shipmentType, storageId, trackCompany.getId(), bundling);
            if (dimensId ==null) {
                continue;
            }
            ids.add(dimensId);

        }

        return ids;
    }


    /**
     * 根据 尺寸 重量和运输公司  获取dimensid
     *
     * @param size
     * @param weight
     * @param storageId
     * @return
     */
//    @Override
//    public List<Long> listBySizeAndWeight(double size, double weight, Long storageId, Long trackId) {
//        return this.listBySizeAndWeight(size, weight, null, null, null, storageId, trackId);
//    }


    /**
     * 添加 最长边 次长边 最短边 上限要求
     * @param size
     * @param weight
     * @param longest
     * @param minor
     * @param shortest
     * @param storageId
     * @param trackId
     * @return
     */
    @Override
    public List<DimensDo> listBySizeAndWeight(double size, double weight, Double longest, Double minor, Double shortest, Integer cod, Integer shipmentType, Long storageId, Long trackId, Integer bundling) {
        List<DimensDo> ids = new ArrayList<>();
        if (trackId == 0) {
            ids = this.listBySizeAndWeight(size, weight, longest, minor, shortest, cod, shipmentType, storageId, bundling);
        } else {
            DimensDo dimensId = baseMapper.queryBySizeAndWeight(size, weight, longest, minor, shortest, cod, shipmentType, storageId, trackId, bundling);
            if (dimensId != null) {
                ids.add(dimensId);
            }
        }

        return ids;
    }
}
